#include "DynamicArray.h"
#include <stdio.h>
#include <string.h>

struct Student
{
    int id;
    char *Name;
};
typedef struct Student Stu;

Stu* CreateStu(int id,const char* name)
{
    Stu *s = (Stu*)malloc(sizeof(Stu));
    if(s == NULL)
    {
        printf("Create malloc error\n");
        exit(-1);
    }
    s->id = id;
    s->Name = strdup(name);
    return s;
}

void ClearStu(void *p)
{
    Stu *s = (Stu*)p;
    free(s->Name);
    free(s);
    //printf("清理\n");
}

//回调函数
void PrintStu(void *p)
{
    Stu *s = (Stu*)p;
    printf("id:%d   name:%s\n",s->id,s->Name);
}

bool IsNameEqual(void *p1,void *p2)
{
    Stu *s = (Stu*)p1;
    const char *name = (const char*)p2;
    return strcmp(s->Name,name) == 0;
}

bool IsIDEqual(void *p1,void *p2)
{
    Stu *s = (Stu*)p1;
    long id = (long)p2;
    return s->id == id;
}

void PrintClass(void *p)
{
    DArray *a = (DArray *)p;
    printDArray(a,PrintStu);
}

void ClearClass(void *p)
{
    DArray *a = (DArray *)p;
    ClearDArray(a,ClearStu);
}

void Split(DArray *a,const char *str,const char *delim)
{
    char *s = strdup(str);
    char *dest = strtok(s,delim);
    while (dest != NULL)
    {
        InsertDArry(a,strdup(dest));
        dest = strtok(NULL,delim);
    }
    free(s);
}

void PrintString(void *p)
{
    printf("%s\n",(char *)p);
}

bool BiggerID(void* p1,void *p2)
{
    Stu *s1 = (Stu*)p1;
    Stu *s2 = (Stu*)p2;
    return s1->id > s2->id;
}

bool SmallerIdID(void* p1,void *p2)
{
    Stu *s1 = (Stu*)p1;
    Stu *s2 = (Stu*)p2;
    return s1->id < s2->id;
}

bool BiggerName(void* p1,void *p2)
{
    Stu *s1 = (Stu*)p1;
    Stu *s2 = (Stu*)p2;
    if( strcmp(s1->Name,s2->Name) > 0)
        return true;
    else
        return false;
}

int IDcmp(void* p1,void *p2)
{
    Stu *s1 = (Stu*)p1;
    return s1->id - (long)p2;
}

int Namecmp(void* p1,void *p2)
{
    Stu *s1 = (Stu*)p1;
    
    return strcmp(s1->Name,(const char*)p2);
}

int main()
{
    DArray* a = InitDArray();
    
    InsertDArry(a,CreateStu(3,"zhangsan"));
    InsertDArry(a,CreateStu(1,"lisi"));
    InsertDArry(a,CreateStu(2,"wangwu"));
    FastSort(a,BiggerName);
    void *s = BinarySearch(a,(void*)3,IDcmp);
    if(s)
    {
        printf("找到了\n");
        PrintStu(s);
    }
    else
        printf("没找到\n");
    printDArray(a,PrintStu);

    // void *s = RemoveByElement(a,(void *)2,IsIDEqual);
    // if(s)
    // {
    //     printf("删除的学生是：");
    //     PrintStu(s);
    //     ClearStu(s);
    // }
    // else
    // {
    //     printf("没找到\n");
    // }
    // printDArray(a,PrintStu);
    // ClearDArray(a,ClearStu);
    // DArray *class1 = InitDArray();
    // InsertDArry(class1,CreateStu(1,"zhangsan"));
    // InsertDArry(class1,CreateStu(2,"lisi"));
    // InsertDArry(class1,CreateStu(3,"wangwu"));

    // DArray *class2 = InitDArray();
    // InsertDArry(class2,CreateStu(4,"liliu"));
    // InsertDArry(class2,CreateStu(5,"qiqi"));
    // InsertDArry(class2,CreateStu(6,"tieniu"));

    // DArray *school = InitDArray();
    // InsertDArry(school,class1);
    // InsertDArry(school,class2);

    // printDArray(school,PrintClass);
    // ClearDArray(school,ClearClass);

    //字符串切割  字符串列表
    // DArray *a = InitDArray();
    // Split(a,"zhangsan,lisi,wangwu",",");
    // //打印字符串
    // printDArray(a,PrintString);
    // ClearDArray(a,free);
    return 0;
}